home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ETO Development Tools 4
/
ETO Development Tools 4.iso
/
Tools - Objects
/
Virtual User 1.0
/
Example Libraries
/
StringsLib.vu
< prev
next >
Wrap
Text File
|
1991-01-25
|
4KB
|
116 lines
#
# File: StringsLibrary.vu
#
# Contains: Task defintions to convert numbers into strings and strings into numbers.
# The main tasks are:
# NumToStr(number) - this task takes in a number and converts it into a
# string. If the argument passed in is not a number undefined is returned.
#
# StrToNum(str, start_index := 1, end_index := -1) - this task takes in
# a string and converts it into an integer, if possible.
# You can pass a start and end indices into a string in which a
# number is embedded. This task deals with both positive and negative numbers.
# It returns MaxNum (32767) or MinNum (-32767) in error(overflow) conditions.
# It deals with cases such as "+123" and "-462" as well as unsigned ones like "981".
# If the argument passed in is not a string undefined is returned.
#
#
# Written by: P Nagarajan
#
# Copyright: © 1990 by Apple Computer, Inc., all rights reserved.
#
# Change History:
#
# 7/16/90 naga added header
#
# To Do:
#
task NumToStr(number)
begin
if (typeOf(number) <> "integer")
return undefined;
return "{number}";
end;#NumToStr
#the following task takes in a string and converts it into an integer
#if possible. You can pass a start and end indices into a string in which a
#number is embedded. This task deals with both positive and negative numbers.
#It returns MaxNum or MinNum values in error(overflow) conditions.
#deals with cases such as "+123" and "-462" as well as "981".
task StrToNum(str, start_index := 1, end_index := -1)
begin
if (typeOf(str) <> "string") return undefined;
MaxNum := 32767; MinNum := -32767; #may be these should be global
if (end_index = -1) end_index := card(str);
if (str[start_index] = '-')
begin
is_negative := true;
start_index := start_index + 1;
end;#check if a negative number
else if (str[start_index] = '+')
begin
start_index := start_index + 1;
end;#check if a positive sign present
if (end_index-start_index) > 4
begin
if is_negative return MinNum;
else return MaxNum;
end;#error case out of size
num := form_positive_integer(str, start_index, end_index);
if (num = -1)
if is_negative return MinNum;
else return MaxNum; #error condition
if is_negative return num*(-1);
else return num;
end; #form an integer from a string
#the following task takes in a string and converts it into a positive integer
#if possible. You can pass a start & end indices into a string in which a number
#is embedded. The string should not be contain more digits than the maximum number
#allowed in VU (ie., 32767)
task form_positive_integer(str, start_index := 1, end_index := -1)
begin
num := 0;
if (end_index = -1) end_index := card(str);
for i:= start_index to end_index
begin
digit := char_to_digit(str[i]);
if (digit = -1) return -1; #error condition
else if (num > 3276) return -1; #overflow condition
else if (num = 3276) and (digit > 7) return -1; #overflow condition
num := num*10 + digit;
end;#for each digit
return num;
end;
#the following task converts a character to its equivalent numeric digit
#if a non-numeric character is passed as the parameter this task returns -1
task char_to_digit(character)
begin
if (character = '0')
return (0);
else if (character = '1')
return(1);
else if (character = '2')
return(2);
else if (character = '3')
return(3);
else if (character = '4')
return(4);
else if (character = '5')
return(5);
else if (character = '6')
return(6);
else if (character = '7')
return(7);
else if (character = '8')
return(8);
else if (character = '9')
return(9);
else return (-1); #error condition
end;